// pages/evaluation/evaluation.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pustlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      id: options.id
    })
    that.onOrderList(options.id);
  },
  //商品列表
  onOrderList: function(id) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/order/my_order_detail", {
      order_id: id
    }, function(res) {
      utils.hideLoading();
      var info = res.datas.snap_items;
      console.log(res.datas.snap_items, "lsajdfk")
      for (var i in info) {
        info[i].star = 5;
        info[i].content = '';
        info[i].image_arr = '';
        info[i].videoid = '';
      }
      that.setData({
        info: info,
      })
    })

  },
  swichNav: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var star = e.currentTarget.dataset.star;
    console.log(index + '-' + star);
    var info = that.data.info;
    info[index].star = star;
    that.setData({
      info: info
    })
  },
  textareaTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var info = that.data.info;
    info[index].content = e.detail.value
    that.setData({
      info: info
    })
  },
  //图片上传
  uploadImg: function(e) {
    var that = this;
    var info = that.data.info;
    var index = e.currentTarget.dataset.index;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
          header: {
            'Content-Type': 'multipart/form-data'
          },
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            key: app.globalData.key
          },
          success: function(result) {
            utils.hideLoading();
            var res = result.data;                     
            //do something
            //上传图片后返回的是字符串，需json.prise一下
            var res = JSON.parse(res);
            var info = that.data.info;
            info[index].image_arr = info[index].image_arr || [];
            info[index].image_arr.push(res);
            // console.log(info)
            that.setData({
              info: info,
            });
          },
          fail: function(res) {
            utils.hideLoading();
            utils.message('上传服务器上传失败,请稍后再试2');
            console.log(res);
          }
        })
      }
    })
  },
  //视频上传
  uploadVideo: function(e) {
    var that = this;
    var info = that.data.info;
    var index = e.currentTarget.dataset.index;

    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function(res) {
        utils.showLoading();
        that.setData({
          src: res.tempFilePath
        })
        wx.uploadFile({
          url: app.globalData.http + '/api/v1/common/upload', //仅为示例，非真实的接口地址
          header: {
            'Content-Type': 'multipart/form-data'
          },
          filePath: res.tempFilePath,
          name: 'file',
          formData: {
            key: app.globalData.key
          },
          success: function(result) {
            utils.hideLoading();
            var res = result.data;
            console.log(JSON.parse(res));
            // var info = that.data.info;
            that.data.info[index].videoid = that.data.info[index].videoid || [];
            that.data.info[index].videoid.push(JSON.parse(res));
            that.setData({
              info: info
            })
            console.log(that.data.info);
          },
          fail: function(res) {
            utils.hideLoading();
            utils.message('上传服务器上传失败,请稍后再试2');
            console.log(res);
          }
        })

      }
    })

  },
  //删除评价图片
  onCloseImgTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var ind = e.currentTarget.dataset.ind;
    var info = that.data.info;
    for (var i in info) {
      for (var j in info[i].image_arr) {
        if (info[i].image_arr[j] == info[index].image_arr[ind]) {
          info[i].image_arr.splice(j, 1)
        }
      }
      that.setData({
        info: info
      })
      console.log(info)
    }
  },
  //删除视频图片
  onCloseVideoTap: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var ind = e.currentTarget.dataset.ind;
    console.log(ind)
    console.log(index)
    var info = that.data.info;
    for (var i in info) {
      for (var j in info[i].videoid) {
        console.log(info[index].videoid[ind])
        if (info[i].videoid[j] == info[index].videoid[ind]) {
          info[i].videoid.splice(j, 1)
        }
      }
      that.setData({
        info: info
      })
      // console.log(info)
    }
  },
  //提交发表评论
  formSubmit: function(e) {
    var that = this;
    var goods_list = [];
    for (var i = 0; i < that.data.info.length; i++) {
      var p = [];
      var vider = [];
      for (var j in that.data.info[i].image_arr) {
        p.push(that.data.info[i].image_arr[j].id);
      }
      for (var j in that.data.info[i].videoid) {
        vider.push(that.data.info[i].videoid[j].id);
      }

      var goods_item = {
        goods_id: that.data.info[i].id,
        content: that.data.info[i].content,
        image: p.join(","),
        star: that.data.info[i].star,
        video: vider.join(","),
      };
      goods_list.push(goods_item)
      if (that.data.info[i].content.length <= 0) {
        var ppp = parseInt(i) + parseInt(1);
        wx.showToast({
          title: '请填写第' + ppp + '条评价',
          icon: "none",
          duration: 2000
        })
        return false;
      }

    }
    // console.log(JSON.stringify(goods_list))
    // return false;
    utils.http("/api/v1/order/make_order_comments", {
      order_id: that.data.id,
      comments_list: JSON.stringify(goods_list)
    }, function(res) {
      utils.hideLoading();
      if (res.state == 1) {
        wx.showToast({
          title: '发表成功',
          duration: 2000
        })
        // wx.redirectTo({
        //     url: '../personal/myorder/myorder?stateid=' + 4 + "&stateindex=" + 4,
        // })
        wx.navigateBack({})
      } else {
        utils.message(res.datas)
      }

    })


  }
})